package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public class CalculateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数?请求参数属于哪个对象所有--->request
        //根据表单数据的key值（name）获取value值，返回值是String类型
        String num1 = request.getParameter("num_1");
        String num2 = request.getParameter("num_2");
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);
        int sum = Integer.parseInt(num1) + Integer.parseInt(num2);
        PrintWriter writer = response.getWriter();
        writer.write(num1 + "+" + num2 + "=" + sum);
        writer.flush();
        writer.close();

    }
}
